Main site

Built-in Modules > Alibaba Class

Alibaba Class

Important: This plugin relies on third-party resources. The resources might change or be discontinued without notice. Ensure you test the plugin extensively before production use, implement error handling with a try-catch block, have a backup shipping rate ready, and reach out to our support with detailed descriptions of any unexpected issues.

Overview

The Alibaba class offers a solution for retrieving real-time shipping rates for products available on Alibaba.

Class Definition

class Alibaba {
  constructor(destination, currency) {
    // Class constructor
  }

  async getRates(alibabaProducts) {
    // Method to fetch rates
  }
}

Arguments

  • destination (Object): This is the "destination" detail from the input DATA JSON object used in the calculateShippingRates function.
  • currency (String): The currency ISO code intended for the shipping rate calculations.

Usage Example

const alibaba = new Alibaba(DATA.destination, DATA.currency);
const alibabaProducts = [
  { alibaba_product_id: 1600943515020, quantity: 10 },
  { alibaba_product_id: 1600141016792, quantity: 100 },
  // ... additional products
];
const rates = await alibaba.getRates(alibabaProducts);

In this usage scenario, the Alibaba class is utilized to fetch shipping rates for Alibaba products, each specified by an alibaba_product_id and quantity. The getRates method computes the shipping fees for these items, with the resulting rates presented in a format consistent with Shopify's shipping rates.

The alibaba_product_id is the last string in the last pathname of the Alibaba product page url. For example, https://www.alibaba.com/product-detail/Plastic-Octopus-Eight-Rows-Black-Hair_1600141016792.html, the product id is 1600141016792.

Limitations

Please note that this plugin generates shipping rates only for Alibaba products that have a shipping cost displayed on the product page. It will generate error if the shipping cost on Alibaba product page is "To be negotiated" or other information.

Sample AI Prompt

I'd like a shipping rate calculator for items from Alibaba products.
The Alibaba product id is contained in the metafield namespace "market" with key "alibaba_id". Enrich the items data with metafield "market" and take the first 10 values.

Sample AI Output

// Import required modules
import { enrichItemDetails, Alibaba } from "./modules.js";

// Function to calculate shipping rates
export async function calculateShippingRates(DATA, env) {
  try {
    // Enrich items data with metafield "market"
    DATA = await enrichItemDetails(DATA, [{ namespace: "market", size: 10 }]);

    // Create an instance of Alibaba class
    const alibaba = new Alibaba(DATA.destination, DATA.currency);

    // Extract Alibaba product IDs and quantities
    const alibabaProducts = DATA.items.map((item) => {
      const alibabaId = item.metafields.market.alibaba_id;
      return { alibaba_product_id: alibabaId, quantity: item.quantity };
    });

    // Get shipping rates from Alibaba
    const rates = await alibaba.getRates(alibabaProducts);

    // Return shipping rates in the expected Shopify format
    return { rates };
  } catch (error) {
    // Handle errors and return an empty rate with price set to null
    return {
      rates: [
        {
          service_name: "",
          service_code: "",
          total_price: null,
          description: "",
          currency: "",
          min_delivery_date: "",
          max_delivery_date: "",
        },
      ],
    };
  }
}